package com.smartsofa.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;

public class MyUtils {
	
	public static int USERID=0;

	public static int getUSERID() {
		return USERID;
	}

	public static void setUSERID(int uSERID) {
		USERID = uSERID;
	}

//	private static String url="http://121.42.151.185:8080/HealthManager/";
//	private static String url="http://localhost:8080/HealthManager/";
	private static String url="http://192.168.1.103:8080/HealthManager/";

	
	public static int REQUEST_TIMEOUT = 10 * 1000;//设置请求超时10秒钟
	
	//计算BMI指数
	public static float getBmiIndex(float height,float weight){
		float bmi=(float) (weight/Math.pow((height/100),2));
		return bmi;
	}
	public static float getBmiIndex(double height, double weight) {
		// TODO Auto-generated method stub
		float bmi=(float) (weight/Math.pow((height/100),2));
		return bmi;
	}
	
	 public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		MyUtils.url = url;
	}

	public static int dip2px(Context context, float dipValue){
	        final float scale = context.getResources().getDisplayMetrics().density;
	        return (int)(dipValue * scale + 0.5f);
	    }

	    public static int px2dip(Context context, float pxValue){
	        final float scale = context.getResources().getDisplayMetrics().density;
	        return (int)(pxValue / scale + 0.5f);
	    }

	    public static int sp2px(Context context, float spValue) {
	        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
	        return (int) (spValue * fontScale + 0.5f);
	    }
	    
		public static boolean isNetworkConnected(Context context) { 
			if (context != null) { 
				ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
				.getSystemService(Context.CONNECTIVITY_SERVICE); 
				NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
				if (mNetworkInfo != null) { 
				return mNetworkInfo.isAvailable(); 
				} 
			} 
			return false; 
			} 

	  /**
		* 读取路径中的图片，然后将其转化为缩放后的bitmap
	    * @param path
	    */
	   public String saveBefore(String path) {
	       BitmapFactory.Options options = new BitmapFactory.Options();
	       options.inJustDecodeBounds = true;
	       // 获取这个图片的宽和高
	       Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空
	       options.inJustDecodeBounds = false;
	       // 计算缩放比
	       int be = (int) (options.outHeight / (float) 200);
	       if (be <= 0)
	           be = 1;
	       options.inSampleSize = 4; // 图片长宽各缩小至四分之一
	       // 重新读入图片，注意这次要把options.inJustDecodeBounds 设为 false哦
	       bitmap = BitmapFactory.decodeFile(path, options);
	       // savePNG_After(bitmap,path);
	       return saveJPGE_After(bitmap, path);
	   }
	   /**
	    * 保存图片为JPEG
	    * @param bitmap
	    * @param path
	    */
	   public  String saveJPGE_After(Bitmap bitmap, String path) {
	       File file = new File(path);
	       try {
	           FileOutputStream out = new FileOutputStream(file);
	           if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
	               out.flush();
	               out.close();
	           }
	       } catch (FileNotFoundException e) {
	           e.printStackTrace();
	       } catch (IOException e) {
	           e.printStackTrace();
	       }
	       return path;
	   }
	   public static String convertIconToString(Bitmap bitmap)  
	   {  
	       ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream  
	       bitmap.compress(CompressFormat.PNG, 100, baos);  
	       byte[] appicon = baos.toByteArray();// 转为byte数组  
	       return Base64.encodeToString(appicon, Base64.DEFAULT);  
	   }
	   //将用户头像存入本地sd卡中
	   public static void setPicToStore(int userid,Bitmap mBitmap) {						
		   String sdStatus = Environment.getExternalStorageState(); 		    			
		   if (sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用           
		       
			   FileOutputStream fOut = null;
			   String fileName = Environment.getExternalStorageDirectory() +"/user_img_"+userid+".png";//图片名字
	
			   File f = new File(fileName);
			   
			   try { 
				   Log.i("start store","true");
				   f.createNewFile();
				   fOut = new FileOutputStream(f);
				   if(fOut != null){
				   mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
				   fOut.flush();
				   fOut.close(); 
				   Log.i("savepng","sucess");
				   }
			   }catch (Exception e){ 
				   e.printStackTrace(); 
			   } 
		   }
		}
	   
	   /**
	    * 圆角图片
	    * @param bitmap
	    * @param pixels
	    * @return
	    */
	   public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  		   
	        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  	 
	        Canvas canvas = new Canvas(output);  	 
	        final int color = 0xff424242;  	 
	        final Paint paint = new Paint();  	 
	        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  	 
	        final RectF rectF = new RectF(rect);  	 
	        final float roundPx = pixels;  	 
	        paint.setAntiAlias(true);  	 
	        canvas.drawARGB(0, 0, 0, 0);  	 
	        paint.setColor(color);  	 
	        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  	 
	        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  	 
	        canvas.drawBitmap(bitmap, rect, rect, paint);
	        return output;  	 
	    }

	   
}
